package com.cqw.commontest.lambda;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: caoqingwen
 * @CreateTime: 2021-06-02 15:04
 * @Description: 排序 sorted 中间操作，有两种排序
 * 1、sorted()：自然排序，流中元素需实现Comparable接口
 * 2、sorted(Comparator com):Comparator排序器自定义排序
 */
public class StreamTest9 {


    public static void main(String[] args) {
        List<Person> personList = new ArrayList<>();
        personList.add(new Person("张三", 1000, 21, "男", "上海"));
        personList.add(new Person("李四", 2000, 25, "女", "安徽"));
        personList.add(new Person("王五", 2200, 36, "男", "杭州"));
        personList.add(new Person("赵六", 1400, 25, "女", "南京"));
        personList.add(new Person("曹七", 3000, 12, "男", "北京"));
        personList.add(new Person("Lily", 5000, 42, "女", "上海"));

        //按工资升序排序（自然排序）
        List<Person> collect = personList.stream().sorted(Comparator.comparing(Person::getSalary)).collect(Collectors.toList());
        //System.out.println(collect);

        //按工资倒叙
        List<Person> collect1 = personList.stream().sorted(Comparator.comparing(Person::getSalary).reversed()).collect(Collectors.toList());
        System.out.println(collect1);


    }


}
